﻿$(document).ready(function() {
    $(".deleteUser").live("click", function(index) {
        var offset = $(this).offset();
        $('#deleteUserDialogContainer').css("top", offset.top - 85);
        $('#deleteUserDialogContainer').css("left", offset.left - 320);
        $('#deleteUserDialogContainer').removeClass("none");
            var username = $(this).parent().prev().prev().prev().prev().prev().prev().text();
            var btn = $(this);
        $('#deleteUserOK').click(function() {
            $.ajax({
                url: "/Account/DeleteUserRow",
                data: ({ username: username }),
                cache: false,
                success: function(response) {
                    btn.parents("tr.userRow:first").remove();
                }
            });
            $('#deleteUserDialogContainer').addClass("none");
            return false;
        });
        $('#deleteUserCancel').click(function() {
            $('#deleteUserDialogContainer').addClass("none");
            return false;
        });
    });

    $('#SelectedRoleName').live("change", function() {
        ApplyFilters();
    });

    $('#SelectedOrderByFilter').live("change", function() {
        ApplyFilters();
    });

    $('#SelectedOrganisation').live("change", function() {
        ApplyFilters();
    });

    $('#SelectedDemographic').live("change", function() {
        ApplyFilters();
    });

    $('#SelectedLocation').live("change", function() {
        ApplyFilters();
    });

    function ApplyFilters() {
        $.ajax({
            url: "/Account/FilterAccounts",
            data: ({ rolename: $('#SelectedRoleName').val(), filter: $('#SelectedOrderByFilter').val(), organisation: $('#SelectedOrganisation').val(), demographic: $('#SelectedDemographic').val(), location: $('#SelectedLocation').val() }),
            cache: false,
            success: function(html) {
                $('.users').html(html);
            }
        });
        return false;
    }
})